<?php

namespace Lib;

class Validate extends \Fuel\Core\Validation {
	
	public function add_fields($fields = array())
	{		
		if(is_array($fields) && count($fields))
		{
			foreach($fields as $field)
			{
				if(isset($field['name']))
				{
					$validate = isset($field['validate']) ? $field['validate'] : array();
					$label = isset($field['label']) ? $field['label'] : '';
					$this->add_field($field['name'], $label, $validate);
				}
			}
		}
	}
	
	public function get_errors($open_tag = '<p>', $close_tag = '</p>')
	{
		$errors = '';
        $error_fields = array ();
        foreach ( $this->error() as $field => $val )
		{
            $error_fields[] = $field;
        }
		
        foreach ( self::$fields as $field )
		{
            if ( in_array($field['name'], $error_fields) )
			{
                if ( isset($field['messages']) )
				{
                    if ( is_array($field['messages']) )
					{
                        foreach ( $field['messages'] as $rule => $msg )
						{
                            if ( $this->error($field['name'])->get_message() !== '' )
							{
                                $errors .= $open_tag . $this->error($field['name'])->get_message($msg) . $close_tag;
                            }
                        }
                    }
                }
				else
				{
                    $errors .= $open_tag . $this->error($field['name']) . $close_tag;
                }
            }
        }

        return $errors;
	}

}